﻿using System;
using System.Web.Mvc;
using Gx.Common;

using Gx.Model;
using Gx.Model.Enums;
using Gx.Bll.Manage;

namespace Gx.ColdWarehouseErp.HelperClass
{

    // 调用方法：[AuthorActionAttributeFilter]

    /// <summary>
    /// 用户是否登陆
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
    public class AuthorActionAttributeFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
            {
                base.OnActionExecuting(filterContext);
                return;
            }
            IAuthorizationFilter filter = new AuthorizationFilter();
            AuthorizationContext authorContext = new AuthorizationContext(filterContext.Controller.ControllerContext,
                filterContext.ActionDescriptor);
            filter.OnAuthorization(authorContext);
            filterContext.Result = authorContext.Result;
            base.OnActionExecuting(filterContext);
        }
    }

    public class AuthorizationFilter : IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            string cookieValue = CookieHelper.GetCookie(LoginHelper.UserCookie);
            if (string.IsNullOrEmpty(cookieValue))
            {
                //跳转到登陆页
                ActionResult result = new RedirectResult(LoginHelper.ExitLogin);
                filterContext.Result = result;
            }
            //string action = filterContext.RouteData.Values["action"].ToString();
            //if (action.ToLower().Contains("del") && LoginHelper.GetRoleId() != (int)Roles.管理员)
            //{
            //    filterContext.Result = new JsonResult()
            //    {
            //        Data = new ReturnModel<string>() { Msg = "权限不足", Code = (int)Code.权限不足 },
            //        JsonRequestBehavior = JsonRequestBehavior.DenyGet
            //    };
            //}
        }
    }

    //调用方法： [AboutErrorAttributeMvc]
    /// <summary>
    /// 错误捕捉
    /// </summary>
    public class AboutErrorAttributeMvc : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            string message = filterContext.Exception.Message;
            LogHelper.WriteErrorLog(message);
            string url = string.Format("/{0}/{1}", filterContext.RouteData.Values["controller"].ToString(), filterContext.RouteData.Values["action"].ToString());
            new M_LogInfoBll().AddErrorLog(url, message);
            //是否AJAX请求
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.Result = new JsonResult()
                {
                    Data = new ReturnModel<string>() { Msg = "服务端错误,请联系系统管理员", Code = (int)Code.ServiceError },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    MaxJsonLength = int.MaxValue
                };
            }
            else
            {
                //ActionResult result = new RedirectResult(UserLoginInfo.ExitLogin);
                //filterContext.Result = result;
                filterContext.Result = new JsonResult()
                {
                    Data = new ReturnModel<string>() { Msg = "服务端错误,请联系系统管理员", Code = (int)Code.ServiceError },
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    MaxJsonLength = int.MaxValue
                };
            }
            filterContext.ExceptionHandled = true;
        }

    }
}